﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Entities = Vision.Core.Entities;
using Components = Vision.Core.Components;

namespace Vision.Web.Pages
{
    public partial class ActivitySchedule : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack){
                int activityId = int.Parse(Request.QueryString["ActivityId"]);
                
                using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                    Entities.Activities.VisionActivity activity = activityCatalog.GetVisionActivityFromCatalog(activityId);
                    this.lblActivtyName.Text = activity.Name;
                    this.lblActivtyDesc.Text = activity.Description;
                    this.lblActivtyName.DataBind();
                    this.lblActivtyDesc.DataBind();
                    
                    using (Components.ActivityScheduler activityScheduler = new Components.ActivityScheduler(ConfigurationManager.AppSettings["VisionCatalog"])){
                        gvActivitySchedules.DataSource = activityScheduler.GetVisionSchedules(activity.Key);
                        gvActivitySchedules.DataBind();
                    }                                
                }                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Activities.aspx");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNewSchedule_Click(object sender, EventArgs e)
        {                        
            Common.QStringBuilder qsBuilder = new Common.QStringBuilder();
            qsBuilder.Parameters.Clear();
            qsBuilder.Path = "~/Pages/ActivityScheduleEdit.aspx";
            qsBuilder.Parameters.Add("ActivityId", Request.QueryString["ActivityId"]);
            qsBuilder.Parameters.Add("ScheduleId", "0");            
            Response.Redirect(qsBuilder.QueryString);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvActivitySchedulesAction_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = (ImageButton)sender;
            if (button != null){
                Common.QStringBuilder qsBuilder = new Common.QStringBuilder();
                
                switch (button.CommandName){
                    case "EditSchedule":                        
                        qsBuilder.Parameters.Clear();
                        qsBuilder.Path = "~/Pages/ActivityScheduleEdit.aspx";
                        qsBuilder.Parameters.Add("ActivityId", Request.QueryString["ActivityId"]);
                        qsBuilder.Parameters.Add("ScheduleId", button.CommandArgument);            
                        Response.Redirect(qsBuilder.QueryString);
                        break;
                    case "DeleteSchedule":                                                
                        using (Components.ActivityScheduler activityScheduler = new Components.ActivityScheduler(ConfigurationManager.AppSettings["VisionCatalog"])){
                            activityScheduler.RemoveVisionScheduleById(int.Parse(button.CommandArgument));    
                        }                                
                        
                        qsBuilder.Parameters.Clear();
                        qsBuilder.Path = "~/Pages/ActivitySchedule.aspx";
                        qsBuilder.Parameters.Add("ActivityId", Request.QueryString["ActivityId"]);                        
                        Response.Redirect(qsBuilder.QueryString);
                        break;
                }
            }
        }
    }
}